package com.hd.swagger0411.controller;

import com.hd.swagger0411.entity.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @ApiOperation("测试3")
    @PostMapping("/test3")
    @ResponseBody
    public User test3( String name,
                       String password,
                       @RequestHeader("token") String token,
                       @RequestBody User user){
        System.out.println(name);
        System.out.println(password);
        System.out.println(token);
        User use1 = new User();
        user.setName("jack");
        user.setPassword("123");
        return use1;
    }
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value ="用户名" , required = true,defaultValue = "张三")
            ,@ApiImplicitParam(name = "password" , required = true)
            })
    @ApiOperation(value = "用户登录接口",tags = "提供用户名和密码，校验登录")
    @GetMapping("/login")
    public User login(@ApiParam( value ="用户名", required = true)
            String name,String password) {
        User user = new User();
        return user;
    }
    @RequestMapping(value = "/delusers",method = RequestMethod.POST)
    public Integer deleteUsers( Integer[] ids ){
        List<Integer> idList = Arrays.asList(ids);
        idList.forEach(System.out::println);
        System.out.println("=====删除用户成功=====");
        return 0 ;
    }
    @DeleteMapping("/del/{id}")
    public Long delbyId(@PathVariable(value = "id") Long id) {
        System.out.println("======删除用户成功=====");
        return id;
    }
    @PutMapping("/edit")
    public User editUser(@RequestBody User user) {
        System.out.println("====" + user);
        System.out.println("=====修改用户成功=====");
        return user;
    }
    @GetMapping("/get")
    public User findById(Integer id) {
        User user = new User();

        user.setId(id);
        user.setName("zhangsan");
        user.setSex("1");

        return user;
    }
    @RequestMapping(value = "/test1",method = RequestMethod.GET)
    public String test1(){
        System.out.println("test1....");
        return "hello swagger";
    }    @RequestMapping(value = "/test2",method = RequestMethod.POST)
    public User test2(){
        System.out.println("getUser....");
        User user = new User();
        user.setName("jack");
        user.setPassword("123");
        return user;
    }
    @GetMapping("/getUser/{id}")
    public User findById(@PathVariable(value = "id") Long id) {
        User user = new User();
        user.setId(Integer.valueOf(id+""));
        user.setPassword("123");
        user.setName("张三");
        user.setRegistTime(new Date());
        user.setBirthday(new Date());
        user.setSex("1");
        return user;
    }
    @GetMapping("/getUserByname")
    public User findByName(String username,Integer age) {
        System.out.println(username);
        User user = new User();
        user.setId(1);
        user.setPassword("123");
        user.setName("张三");
        user.setRegistTime(new Date());
        user.setBirthday(new Date());
        user.setSex("1");
        return user;
    }
    @GetMapping("/findAll")
    public List<User> findUserAll() {
        //1. 创建用户集合对象
        List<User> userList = new ArrayList<>();
        //2. 返回用户集合对象
        return userList;
    }
    // @RequestBody 注解的作用，把请求体中的json转换为User对象
    // 注意;需要json的字段名和User的属性对应
    //@RequestBody和@ResponseBody 的区别+
    @PostMapping("/add")
    public User addUser(@RequestBody User user) {
        System.out.println("====" + user);
        System.out.println("=====添加用户成功=====");
        return user;
    }

}
